package com.meiqiongproperty.yiguanjia.fragment.order;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.OrderDetailLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnClick;
import com.meiqiongproperty.yiguanjia.fragment.order.orderAdapter.OrderDetailListAdapter;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendOrderDetailList;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendShopOrderPay;
import com.meiqiongproperty.yiguanjia.httpbean.order.OrderDetailListBean;
import com.meiqiongproperty.yiguanjia.httpbean.order.ShopOrderRepayWXBean;
import com.meiqiongproperty.yiguanjia.httpbean.order.ShopOrderRepayZFBBean;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Administrator
 * date: 2017/6/15.
 * package:com.meiqiongproperty.yiguanjia.fragment.order.orderAdapter
 */

public class OrderDetailFragment extends BaseFragment {
    OrderDetailLayoutBinding binding;
    int orderType;
    String orderId;
    List<OrderDetailListBean.OrderdetailBean.ShopOrderInfoBean> list_order_info = new ArrayList<>();
    OrderDetailListAdapter adapter;
    String paystatus;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.order_detail_layout, container, false);
        binding.setClick(this);
        binding.navigation.setTitle("订单详情");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });

        savedInstanceState = getArguments();
        if (savedInstanceState != null) {
            orderType = savedInstanceState.getInt("OrderType");
            orderId = savedInstanceState.getString("orderId");
        }
        binding.confirmOrderProductList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        getOrderDetail();
        return binding.getRoot();
    }


    public void submitOrderClick(View view) {
        if (Utils.isFastDoubleClick()) return;
        orderShopPay();
    }

    /**
     * 获取订单详情数据
     */
    public void getOrderDetail() {
        SendOrderDetailList send = new SendOrderDetailList();
        send.setToken(Utils.getToken(getActivity()));
        send.setRoomID(Utils.getRoomID(getActivity()));
        send.setOrderType(orderType + "");
        send.setOrderId(orderId);

        ApiService.OrderDetailList(getActivity(), new IHttpCallback<OrderDetailListBean>() {
            @Override
            public void onNext(OrderDetailListBean orderDetailListBean) {
                list_order_info.addAll(orderDetailListBean.getOrderdetail().getShopOrderInfo());
                adapter = new OrderDetailListAdapter(getActivity(), list_order_info);
                binding.confirmOrderProductList.setAdapter(adapter);
                orderId=orderDetailListBean.getOrderdetail().getOrderId();

                binding.orderTime.setText("下单时间：" + orderDetailListBean.getOrderdetail().getCreateAt());
                binding.orderNumber.setText("订单编号：" + orderDetailListBean.getOrderdetail().getOrderId());
                binding.orderTotalPrice.setText("￥" + orderDetailListBean.getOrderdetail().getOrderPrice());
                binding.tvOrderConsignee.setText("收货人：" + orderDetailListBean.getOrderdetail().getUserName());
                binding.tvOrderPhone.setText("电话：" + orderDetailListBean.getOrderdetail().getUserPhone());
                binding.tvOrderAddress.setText("收货地址：" + orderDetailListBean.getOrderdetail().getAddress());
                binding.tvDeliveryTime.setText(orderDetailListBean.getOrderdetail().getDeliverType());
                binding.tvPayType.setText(orderDetailListBean.getOrderdetail().getPayMethodName() + "支付(" + orderDetailListBean.getOrderdetail().getPayStatusName() + ")");

                paystatus = orderDetailListBean.getOrderdetail().getPayStatus();
                if (paystatus.equals("1")) {
                    binding.llShopPay.setVisibility(View.VISIBLE);
                } else {
                    binding.llShopPay.setVisibility(View.GONE);
                }
                adapter.setOnClickListener(new OnClick() {
                    @Override
                    public void onClick(View view, int position) {

                    }
                });
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(send));
    }

    /**
     * 商品订单支付
     */
    public void orderShopPay() {
        SendShopOrderPay send = new SendShopOrderPay();
        send.setToken(Utils.getToken(getActivity()));
        send.setRoomID(Utils.getRoomID(getActivity()));
        send.setOrderId(orderId);
        send.setOrderType(orderType + "");

        switch (paystatus) {
            case "1":
                ApiService.OrderShopZFBPay(getActivity(), new IHttpCallback<ShopOrderRepayZFBBean>() {
                    @Override
                    public void onNext(ShopOrderRepayZFBBean shopOrderRepayZFBBean) {
                        LogUtil.d("wxdfdfddfdfd====>支付宝调用成功");
                        String orderInfo = shopOrderRepayZFBBean.getParameter().getParameter();
                        doAlipay(orderInfo);
                    }

                    @Override
                    public void onError(int code, String message) {

                    }
                }, Utils.getRequestBody(send));
                break;
            case "2":
                ApiService.OrderShopWXPay(getActivity(), new IHttpCallback<ShopOrderRepayWXBean>() {
                    @Override
                    public void onNext(ShopOrderRepayWXBean shopOrderRepayWXBean) {
                        LogUtil.d("wxdfdfddfdfd====>微信调用成功");
                        doShopOrderWXPay(shopOrderRepayWXBean.getParameter().getParameter(), shopOrderRepayWXBean.getParameter().getParameter().getAppid());
                    }

                    @Override
                    public void onError(int code, String message) {
                        LogUtil.d("wxdfdfddfdfd====>微信调用失败"+message);
                    }
                }, Utils.getRequestBody(send));
                break;
        }
    }

}
